package com.yuelife.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuelife.service.dao.SCollectionMapper;
import com.yuelife.service.pojo.SCollection;
import com.yuelife.service.service.SCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:SCollection业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SCollectionServiceImpl implements SCollectionService {

    @Autowired
    private SCollectionMapper sCollectionMapper;


    /**
     * SCollection条件+分页查询
     * @param sCollection 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<SCollection> findPage(SCollection sCollection, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(sCollection);
        //执行搜索
        return new PageInfo<SCollection>(sCollectionMapper.selectByExample(example));
    }

    /**
     * SCollection分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<SCollection> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<SCollection>(sCollectionMapper.selectAll());
    }

    /**
     * SCollection条件查询
     * @param sCollection
     * @return
     */
    @Override
    public List<SCollection> findList(SCollection sCollection){
        //构建查询条件
        Example example = createExample(sCollection);
        //根据构建的条件查询数据
        return sCollectionMapper.selectByExample(example);
    }


    /**
     * SCollection构建查询对象
     * @param sCollection
     * @return
     */
    public Example createExample(SCollection sCollection){
        Example example=new Example(SCollection.class);
        Example.Criteria criteria = example.createCriteria();
        if(sCollection!=null){
            // 主键(收藏
            if(!StringUtils.isEmpty(sCollection.getCollectionId())){
                    criteria.andEqualTo("collectionId",sCollection.getCollectionId());
            }
            // 商品id
            if(!StringUtils.isEmpty(sCollection.getServiceId())){
                    criteria.andEqualTo("serviceId",sCollection.getServiceId());
            }
            // 业主id
            if(!StringUtils.isEmpty(sCollection.getUid())){
                    criteria.andEqualTo("uid",sCollection.getUid());
            }
            // 收藏时间
            if(!StringUtils.isEmpty(sCollection.getCollctionTime())){
                    criteria.andEqualTo("collctionTime",sCollection.getCollctionTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        sCollectionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改SCollection
     * @param sCollection
     */
    @Override
    public void update(SCollection sCollection){
        sCollectionMapper.updateByPrimaryKey(sCollection);
    }

    /**
     * 增加SCollection
     * @param sCollection
     */
    @Override
    public void add(SCollection sCollection){
        sCollectionMapper.insert(sCollection);
    }

    /**
     * 根据ID查询SCollection
     * @param id
     * @return
     */
    @Override
    public SCollection findById(Integer id){
        return  sCollectionMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询SCollection全部数据
     * @return
     */
    @Override
    public List<SCollection> findAll() {
        return sCollectionMapper.selectAll();
    }
}
